package com.soundShape.mediators.referrals
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.popUps.ReferralPopUp;
	import com.soundShape.views.referrals.Referrals;
	import com.soundShape.vo.CustomerVO;
	import com.soundShape.vo.ReferralVO;
	import com.soundShape.vo.SpecialDateVO;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import nz.co.codec.flexorm.criteria.Criteria;
	
	import spark.collections.SortField;
	import spark.events.IndexChangeEvent;
	
	
	
	
	public class ReferralsMediator extends AbstractViewMediator
	{
		
				
				public function ReferralsMediator()
				{
					super(Referrals);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Referrals
				{
					return this._view as Referrals;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				
				private function setViews():void
				{
					var criteria:Criteria;
					
					//get all referrals
					criteria = applicationModel.entityManager.createCriteria(ReferralVO);
					var referrals:ArrayCollection = this.applicationModel.entityManager.fetchCriteria(criteria);
					this.view.dgReferrals.dataProvider = referrals;
					
					//get all customers
					criteria = applicationModel.entityManager.createCriteria(CustomerVO);
					var customers:ArrayCollection = this.applicationModel.entityManager.fetchCriteria(criteria);
					this.view.ddCustomers.dataProvider = customers;
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					
					this.view.dgReferrals.addEventListener(ListEvent.ITEM_CLICK, handleItemClick);
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					//this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.ddCustomers.addEventListener(IndexChangeEvent.CHANGE,handleDDCustomersChange);
				}
				
								
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				protected function handleItemClick(listEvent:ListEvent):void
				{
					var referralVO:ReferralVO = ArrayCollection(this.view.dgReferrals.dataProvider).getItemAt(listEvent.rowIndex) as ReferralVO;
					
					//get customers for referrals combobox
					var criteria:Criteria = applicationModel.entityManager.createCriteria(CustomerVO);
					var customers:ArrayCollection = this.applicationModel.entityManager.fetchCriteria(criteria);
					
					var referralPopUp:ReferralPopUp = new ReferralPopUp();
					referralPopUp.applicationModel = this.applicationModel;
					referralPopUp.collCustomers = customers;
					referralPopUp.referralVO = referralVO;
					referralPopUp.viewState = "update";
					referralPopUp.callback = callbackReferral;
					PopUpManager.addPopUp(referralPopUp,this.view,true);
					PopUpManager.centerPopUp(referralPopUp);
				}
				
				public function callbackReferral(referralVO:ReferralVO=null):void
				{
					// referralVO is null if the window is closed with no create/update
					// yet the view will be refreshed
					if(referralVO != null)
					{
						if(referralVO.email == "DELETE") // this is an artificial flag inserted in the 'email' field
							this.applicationModel.entityManager.remove(referralVO);
						else
							this.applicationModel.entityManager.save(referralVO);
					}
					
					//refresh view
					setViews();
				}
				
				protected function handleDDCustomersChange(event:IndexChangeEvent):void
				{
					var customerVO:CustomerVO = ArrayCollection(this.view.ddCustomers.dataProvider).getItemAt(event.newIndex) as CustomerVO;
					
					var referralPopUp:ReferralPopUp = new ReferralPopUp();
					referralPopUp.customerVO = customerVO;
					referralPopUp.collCustomers = ArrayCollection(this.view.ddCustomers.dataProvider);
					referralPopUp.callback = callbackReferral;
					referralPopUp.viewState = "new";
					referralPopUp.applicationModel = this.applicationModel;
					PopUpManager.addPopUp(referralPopUp,this.view,true);
					PopUpManager.centerPopUp(referralPopUp);
				}
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				
				
			
		}
		
	}
